package com.accesshollywood.grfxOrdering.basics;

import java.awt.Dialog;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.grfxOrdering.GRFXOrdersMain;
import com.accesshollywood.grfxOrdering.Dialogs.NewPersonDialog;
import com.accesshollywood.indexables.User;
import com.accesshollywood.models.CloneableComboBoxModel;

public class Users {
	public static HashMap<Integer, User>	allUsers;				// allUsers.get(0)
																	// returns
																	// 'unassigned'
	public static HashMap<Long, User>		allUsersbySSO;			// allUsers.get(0)
																	// returns
																	// 'unassigned'
	public static CloneableComboBoxModel		artistUsers;
	public static CloneableComboBoxModel		producerUsers;
	public static CloneableComboBoxModel		researchUsers;
	public static CloneableComboBoxModel		photoEditUsers;
	public static CloneableComboBoxModel		adminUsers;
	public static CloneableComboBoxModel		legalUsers;
	public static CloneableComboBoxModel		orderedByUsers;
	public static CloneableComboBoxModel		footageUsers;

	public static User						unAssigned;
	public static User						unAssignedArtist;
	public static User						unAssignedProducer;
	public static User						unAssignedResearch;
	public static User						unAssignedPhotoEdit;
	public static User						unAssignedAdmin;
	public static User						unAssignedLegal;
	public static User						unAssignedOrderer;
	public static User						unAssignedFootage;

	public static User						currentUser	= null;

	public static boolean getCurrentUser() {
		try {
			long SSO = Long.parseLong(System.getProperty("user.name"));
			if (Users.allUsersbySSO.containsKey(SSO)) {
				currentUser = Users.allUsersbySSO.get(SSO);
				//Trace.info(Users.currentUser.fullName());
				GFXSQL.alterInt("Connection", "idPerson", Users.currentUser.getId(), GFXSQL.idIP);

				InetAddress ownIP = InetAddress.getLocalHost();
				Users.currentUser.setIP_local(ownIP.getHostAddress());
				
				return true;
			} else {
				NewPersonDialog newPersonDialog = new NewPersonDialog(true, GRFXOrdersMain.gui, Dialog.ModalityType.MODELESS);
				if (newPersonDialog.newUserPointer == null) {
					//Trace.info("cancel...");
					return false;
				} else {
					//Trace.info("here we go.");
					currentUser = newPersonDialog.newUserPointer;
					Users.populateUsers();
					GFXSQL.alterInt("Connection", "idPerson", Users.currentUser.getId(), GFXSQL.idIP);

					InetAddress ownIP = InetAddress.getLocalHost();
					Users.currentUser.setIP_local(ownIP.getHostAddress());
					
					return true;
				}
			}
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void populateUsers() {
		allUsers = new HashMap<Integer, User>();
		allUsersbySSO = new HashMap<Long, User>();
		artistUsers = new CloneableComboBoxModel();
		producerUsers = new CloneableComboBoxModel();
		researchUsers = new CloneableComboBoxModel();
		photoEditUsers = new CloneableComboBoxModel();
		adminUsers = new CloneableComboBoxModel();
		legalUsers = new CloneableComboBoxModel();
		orderedByUsers = new CloneableComboBoxModel();
		footageUsers = new CloneableComboBoxModel();
		try {
			ResultSet users = GFXSQL.getResultSet("person", "*", "");
			while (users.next()) {
				User currUser = new User();
				currUser.fromSQL(users.getInt("idPerson"));
				/*
				InetAddress ownIP = InetAddress.getLocalHost();
				currUser.IP_local = ownIP.getHostAddress();
				currUser.idPerson = users.getInt("idPerson");
				currUser.SSO = users.getLong("SSO");
				currUser.firstName = users.getString("firstName");
				currUser.lastName = users.getString("lastName");
				currUser.emailAddress = users.getString("emailAddress");
				currUser.isOrderedBy = users.getBoolean("isOrderer");
				currUser.isResearch = users.getBoolean("isResearch");
				currUser.isLegal = users.getBoolean("isLegal");
				currUser.isPhotoEdit = users.getBoolean("isPhotoEdit");
				currUser.isFootage = users.getBoolean("isFootage");
				currUser.isArtist = users.getBoolean("isArtist");
				currUser.isAdmin = users.getBoolean("isAdmin");
				currUser.isProducer = users.getBoolean("isProducer");
				currUser.isGuest = users.getBoolean("isGuest");
				//Trace.info(currUser.fullName());
				*/
				
				addUserToLists(currUser);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		unAssigned = allUsers.get(0);
		unAssignedArtist = allUsers.get(3);
		unAssignedProducer = allUsers.get(4);
		unAssignedResearch = allUsers.get(1);
		unAssignedPhotoEdit = allUsers.get(2);
		unAssignedAdmin = allUsers.get(8);
		unAssignedLegal = allUsers.get(5);
		unAssignedOrderer = allUsers.get(9);
		unAssignedFootage = allUsers.get(10);
	}
	
	public static void addUserToLists(User currUser) {
		allUsers.put(currUser.getId(), currUser);
		allUsersbySSO.put(currUser.getSSO(), currUser);
		if (currUser.isOrderer()) {
			addUserToSubList(orderedByUsers, currUser);
		}
		if (currUser.isResearch()) {
			addUserToSubList(researchUsers, currUser);
		}
		if (currUser.isLegal()) {
			addUserToSubList(legalUsers, currUser);
		}
		if (currUser.isPhotoEdit()) {
			addUserToSubList(photoEditUsers, currUser);
		}
		if (currUser.isFootage()) {
			addUserToSubList(footageUsers, currUser);
		}
		if (currUser.isArtist()) {
			addUserToSubList(artistUsers, currUser);
		}
		if (currUser.isAdmin()) {
			addUserToSubList(adminUsers, currUser);
		}
		if (currUser.isProducer()) {
			addUserToSubList(producerUsers, currUser);
		}
	}

	public static void addUserToSubList(CloneableComboBoxModel userList, User currUser) {
		if (userList.getSize() > 0) {
			userList.insertElementAt(currUser, userList.getSize() - 1);
		} else {
			userList.insertElementAt(currUser, userList.getSize());
		}
	}

	public static void addUserToSQL(User currUser) {
		currUser.toSQL();
//		GFXSQL.addPerson(currUser);
	}
}
